﻿// ------------------------------------------------------------------------------------
//      Copyright (c) 2013 uhavemyword@gmail.com. All rights reserved.
//      Created by Ben at 1/3/2013 10:48:28 PM
// ------------------------------------------------------------------------------------

namespace CP.NLayer.Client.WpfClient.Common
{
    using System;
    using System.ComponentModel;
    using System.Linq.Expressions;
    using System.Windows.Threading;
    using CP.NLayer.Models;

    public abstract class ViewModelBase : DataErrorInfo, INotifyPropertyChanged
    {
        #region INotifyPropertyChanged
        /// <summary>
        /// Raised when a property on this object has a new value.
        /// </summary>        
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        public static void InvokeOnUIThread(Action action)
        {
            Dispatcher currentDispatcher = Dispatcher.CurrentDispatcher;
            if (!currentDispatcher.CheckAccess())
            {
                currentDispatcher.BeginInvoke(action);
                return;
            }
            else
            {
                action();
                return;
            }
        }

        /// <summary>
        /// Raises this object's PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">The property that has a new value.</param>
        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler propertyChangedEventHandler = this.PropertyChanged;
            if (propertyChangedEventHandler != null)
            {
                propertyChangedEventHandler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Raises this object's PropertyChanged event for each of the properties.
        /// </summary>
        /// <param name="propertyNames">The properties that have a new value.</param>
        protected void RaisePropertyChanged(params string[] propertyNames)
        {
            if (propertyNames != null)
            {
                string[] strArrays = propertyNames;
                for (int i = 0; i < (int)strArrays.Length; i++)
                {
                    string str = strArrays[i];
                    this.RaisePropertyChanged(str);
                }
                return;
            }
            else
            {
                throw new ArgumentNullException("propertyNames");
            }
        }

        /// <summary>
        /// Raises this object's PropertyChanged event.
        /// </summary>
        /// <typeparam name="T">The type of the property that has a new value</typeparam>
        /// <param name="propertyExpression">A Lambda expression representing the property that has a new value.</param>
        protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
        {
            var memberExpression = propertyExpression.Body as MemberExpression;
            if (memberExpression != null)
            {
                string propertyName = memberExpression.Member.Name;
                this.RaisePropertyChanged(propertyName);
            }
        }
    }
}
